package com.yaozheng.vocationaltraining.widget;

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;
import android.widget.LinearLayout;

import com.yaozheng.vocationaltraining.R;
import com.yaozheng.vocationaltraining.utils.ControlsUtils;
import com.yaozheng.vocationaltraining.utils.LogUtils;
import com.yaozheng.vocationaltraining.utils.TypeUtils;

import net.sf.json.JSONArray;

/**
 * 记忆财富统计view
 */
public class MemoryWealthStatisticsView1 extends View {

    /**
     * 内圆进度画笔对象
     */
    private Paint insideProgressPaint;
    /**
     * 内圆没进度画笔对象
     */
    private Paint insidePaint;

    private Paint reviewPaint;

    private Paint textReviewPaint;

    private Paint textInsidePaint;
    /**
     * 点画笔
     */
    private Paint dotPoint;
    /**
     * 边框大小
     */
    float progressBorder;
    /**
     * 当前进度
     */
//    private float currProgress;

    int max = 100;

    int[] sizes;

    int screenWdith;

    int padding;

    /**
     * 默认小圆颜色
     */
    int defaultRoundColor;
    /**
     * 进度圆颜色
     */
    int scheduleRoundColor;
    /**
     * 大圆颜色
     */
    int bigRoundColor;

    JSONArray dataValue;

    public MemoryWealthStatisticsView1(Context context) {
        super(context);
        initView();
    }

    public MemoryWealthStatisticsView1(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView();
    }


    public void initView() {
        /**
         * 默认小圆颜色
         */
        defaultRoundColor = Color.parseColor("#CCCCCC");
        /**
         * 进度圆颜色
         */
        scheduleRoundColor = Color.parseColor("#999999");
        /**
         * 大圆颜色
         */
        bigRoundColor = Color.parseColor("#E9E8E9");


        progressBorder = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
                2, getResources().getDisplayMetrics());
        padding = getResources().getDimensionPixelOffset(R.dimen.default_margin);
        insideProgressPaint = new Paint();
        insideProgressPaint.setColor(Color.parseColor("#c51949"));

//                getResources().getColor(
//                R.color.progress_select_border_color));
        insideProgressPaint.setAntiAlias(true);// 设置抗锯齿;;;;
        insideProgressPaint.setStyle(Paint.Style.STROKE);// 设置空心.
        insideProgressPaint.setStrokeWidth(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
                1, getResources().getDisplayMetrics()));


        insidePaint = new Paint();
        insidePaint.setColor(Color.parseColor("#b7b7b7"));
//                getResources().getColor(
//                R.color.progress_not_select_border_color));
        insidePaint.setAntiAlias(true);// 设置抗锯齿;;;;
        insidePaint.setStyle(Paint.Style.STROKE);// 设置空心.
        insidePaint.setStrokeWidth(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
                2, getResources().getDisplayMetrics()));


        dotPoint = new Paint();
        dotPoint.setAntiAlias(true);// 设置抗锯齿;;;;
        dotPoint.setColor(Color.parseColor("#000000"));
        dotPoint.setStyle(Paint.Style.FILL);// 设置
        dotPoint.setStrokeWidth(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
                2, getResources().getDisplayMetrics()));
        try {
            sizes = ControlsUtils.getCurrScreenSize1((Activity) getContext());
            screenWdith = sizes[1];
        } catch (Exception ex) {
            screenWdith = 480;
        }


        reviewPaint = new Paint();
        reviewPaint.setAntiAlias(true);// 设置抗锯齿;;;;
        reviewPaint.setColor(Color.parseColor("#BEC2C8"));
        reviewPaint.setStyle(Paint.Style.FILL);// 设置


        textReviewPaint = new Paint();
        textReviewPaint.setAntiAlias(true);// 设置抗锯齿;;;;
        textReviewPaint.setColor(Color.WHITE);
//        textReviewPaint.setStyle(Paint.Style.FILL);// 设置
        textReviewPaint.setTextSize(getResources().getDimensionPixelSize(R.dimen.edit_font_size));


        textInsidePaint = new Paint();
        textInsidePaint.setAntiAlias(true);// 设置抗锯齿;;;;
        textInsidePaint.setColor(Color.parseColor("#c51949"));
//        textReviewPaint.setStyle(Paint.Style.FILL);// 设置
        textInsidePaint.setTextSize(getResources().getDimensionPixelSize(R.dimen.button_font_size));


//        currProgress = 50;

    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.setMeasuredDimension(screenWdith, screenWdith);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        int width = screenWdith;
        float smallRoundWdith = width / 6 / 2;
        float progressRoundWdith = width / 2;
        /**
         * 弧形
         */
        float progressRoundPadding = width / 2;// (width / 2) - smallRoundWdith / 2;
//        RectF progressRoundOval = new RectF(progressRoundPadding - progressRoundWdith / 2 - progressBorder / 2, progressRoundPadding - (progressRoundWdith) / 2 - progressBorder / 2, progressRoundPadding + progressRoundWdith / 2 + progressBorder / 2, progressRoundPadding + progressRoundWdith / 2 + progressBorder / 2);

        RectF progressRoundOval = new RectF(progressRoundPadding - progressRoundWdith / 2 - insideProgressPaint.getStrokeWidth() / 2, progressRoundPadding - (progressRoundWdith) / 2 - insideProgressPaint.getStrokeWidth() / 2, progressRoundPadding + progressRoundWdith / 2 + insideProgressPaint.getStrokeWidth() / 2, progressRoundPadding + progressRoundWdith / 2 + insideProgressPaint.getStrokeWidth() / 2);
//        RectF oval = new RectF(smallRoundPadding-smallRoundWdith/2,smallRoundPadding- smallRoundWdith/2, smallRoundPadding + smallRoundWdith / 2, smallRoundPadding + smallRoundWdith / 2);

        int benginstartAngle = 75;
        /**
         * 红色进度弧形
         */
//        canvas.drawArc(progressRoundOval, 360 * benginstartAngle / max, 360 * currProgress
//                / max, false, insideProgressPaint);
        canvas.drawArc(progressRoundOval, 360 * benginstartAngle / max, 360 * 100
                / max, false, insideProgressPaint);
//        if (currProgress != 100) {
//            /**
//             * 灰色进度弧形
//             */
//            canvas.drawArc(progressRoundOval, 360 * (currProgress + benginstartAngle) / max, 360
//                    * (100 - currProgress - 4) / max, false, insidePaint);
//        }

        float x = width / 2 - progressRoundWdith / 2,
                y = width / 2 - progressRoundWdith / 2;

        float r = progressRoundWdith / 2;
        float a = x + r, b = y + r;
        float x1 = r, y1 = 0, x2 = r * 2, y2 = r, x3 = r, y3 = r * 2, x4 = 0, y4 = r,
                x5 = r, y5 = 0, x6 = r * 2, y6 = r, x7 = r, y7 = r * 2, x8 = 0, y8 = r;
        x1 = r + x;
        y1 = 0 + y;
        x2 = r * 2 + x;
        y2 = r + y;
        x3 = r + x;
        y3 = r * 2 + y;
        x4 = 0 + x;
        y4 = r + y;
        x5 = (float) (a + r * 1.4142 / 2);
        y5 = (float) (b - r * 1.4142 / 2);
        x6 = (float) (a + r * 1.4142 / 2);
        y6 = (float) (b + r * 1.4142 / 2);
        x7 = (float) (a - r * 1.4142 / 2);
        y7 = (float) (b + r * 1.4142 / 2);
        x8 = (float) (a - r * 1.4142 / 2);
        y8 = (float) (b - r * 1.4142 / 2);


//        canvas.drawCircle(x1 + (progressBorder / 2), y1 + (progressBorder), progressBorder, dotPoint);
//        canvas.drawCircle(x2 - progressBorder, y2 + (progressBorder / 2), progressBorder, dotPoint);
//        canvas.drawCircle(x3 + (progressBorder / 2), y3 - progressBorder, progressBorder, dotPoint);
//        canvas.drawCircle(x4 + (progressBorder), y4 - (progressBorder / 2), progressBorder, dotPoint);
//
//
//        canvas.drawCircle(x5 - (progressBorder), y5 + (progressBorder / 2), progressBorder, dotPoint);
//        canvas.drawCircle(x6 - (progressBorder / 2), y6 - (progressBorder), progressBorder, dotPoint);
//        canvas.drawCircle(x7 + (progressBorder / 2), y7 - (progressBorder), progressBorder, dotPoint);
//        canvas.drawCircle(x8 + (progressBorder), y8 + (progressBorder / 2), progressBorder, dotPoint);
        canvas.drawCircle(x1, y1 - (progressBorder / 2) + 1, progressBorder, dotPoint);  //学习
//        if (currProgress >= 37.5) {
        if (TypeUtils.getJsonBoolean(dataValue, 2)) {
            canvas.drawCircle(x2 + (progressBorder / 2) - 1, y2, progressBorder, dotPoint); //1小时
        }
//        if (currProgress >= 50) {
        if (TypeUtils.getJsonBoolean(dataValue, 4)) {
            canvas.drawCircle(x3, y3 + (progressBorder / 2) - 1, progressBorder, dotPoint);//1天
        }
//        if (currProgress >= 75) {
        if (TypeUtils.getJsonBoolean(dataValue, 6)) {
            canvas.drawCircle(x4 - (progressBorder / 2) + 1, y4, progressBorder, dotPoint);//6天
        }
//        if (currProgress >= 12.5) {
        if (TypeUtils.getJsonBoolean(dataValue, 1)) {
            canvas.drawCircle(x5 + (progressBorder / 2), y5, progressBorder, dotPoint);//20分钟
        }
//        if (currProgress >= 62.5) {
        if (TypeUtils.getJsonBoolean(dataValue, 3)) {
            canvas.drawCircle(x6 + (progressBorder / 2), y6, progressBorder, dotPoint);//9小时
        }
//        if (currProgress >= 87.5) {
        if (TypeUtils.getJsonBoolean(dataValue, 5)) {
            canvas.drawCircle(x7 - (progressBorder / 2), y7, progressBorder, dotPoint);//2天
        }
//        if (currProgress >= 100) {
        if (TypeUtils.getJsonBoolean(dataValue, 7)) {
            canvas.drawCircle(x8 - (progressBorder / 2), y8, progressBorder, dotPoint);//1一个月
        }


        float bigPadding = smallRoundWdith + TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
                40, getResources().getDisplayMetrics());
        /**
         * 大圆
         */
        float bigRound = width - smallRoundWdith / 2 - bigPadding;
        x = width / 2 - bigRound / 2; //width / 2 - bigRound / 2 + smallRoundWdith / 2;
        y = width / 2 - bigRound / 2;// width / 2 - bigRound / 2 + smallRoundWdith / 2;
        r = bigRound / 2;
        a = x + r;
        b = y + r;

        x1 = r + x;
        y1 = 0 + y;
        x2 = r * 2 + x;
        y2 = r + y;
        x3 = r + x;
        y3 = r * 2 + y;
        x4 = 0 + x;
        y4 = r + y;
        x5 = (float) (a + r * 1.4142 / 2);
        y5 = (float) (b - r * 1.4142 / 2);
        x6 = (float) (a + r * 1.4142 / 2);
        y6 = (float) (b + r * 1.4142 / 2);
        x7 = (float) (a - r * 1.4142 / 2);
        y7 = (float) (b + r * 1.4142 / 2);
        x8 = (float) (a - r * 1.4142 / 2);
        y8 = (float) (b - r * 1.4142 / 2);


//        canvas.drawCircle(x1 + (progressBorder / 2), y1 + (progressBorder), smallRoundWdith / 2, reviewPaint);
//        canvas.drawCircle(x2 - progressBorder, y2 + (progressBorder / 2), smallRoundWdith / 2, reviewPaint);
//        canvas.drawCircle(x3 + (progressBorder / 2), y3 - progressBorder, smallRoundWdith / 2, reviewPaint);
//        canvas.drawCircle(x4 + (progressBorder), y4 - (progressBorder / 2), smallRoundWdith / 2, reviewPaint);
//
//
//        canvas.drawCircle(x5 - (progressBorder), y5 + (progressBorder / 2), smallRoundWdith / 2, reviewPaint);
//        canvas.drawCircle(x6 - (progressBorder / 2), y6 - (progressBorder), smallRoundWdith / 2, reviewPaint);
//        canvas.drawCircle(x7 + (progressBorder / 2), y7 - (progressBorder), smallRoundWdith / 2, reviewPaint);
//        canvas.drawCircle(x8 + (progressBorder), y8 + (progressBorder / 2), smallRoundWdith / 2, reviewPaint);
//        1  	2 	3      4   5   6   7       8
//        1       3       5      7   2   4   6       8
//        12.5	25	37.5  50  62.5 75  87.5   100
//        12.5    37.5   62.5   87.5  25  50  75   100

        reviewPaint.setColor(scheduleRoundColor);
        canvas.drawCircle(x1, y1, smallRoundWdith, reviewPaint); //学习


//        if (currProgress >= 25) {
        if (TypeUtils.getJsonBoolean(dataValue, 2)) {
            reviewPaint.setColor(scheduleRoundColor);
        } else {
            reviewPaint.setColor(defaultRoundColor);
        }
        canvas.drawCircle(x2, y2, smallRoundWdith, reviewPaint); //1小时


//        if (currProgress >= 50) {
        if (TypeUtils.getJsonBoolean(dataValue, 4)) {
            reviewPaint.setColor(scheduleRoundColor);
        } else {
            reviewPaint.setColor(defaultRoundColor);
        }
        canvas.drawCircle(x3, y3, smallRoundWdith, reviewPaint); //1天
//        if (currProgress >= 75) {
        if (TypeUtils.getJsonBoolean(dataValue, 6)) {
            reviewPaint.setColor(scheduleRoundColor);
        } else {
            reviewPaint.setColor(defaultRoundColor);
        }
        canvas.drawCircle(x4, y4, smallRoundWdith, reviewPaint);//6天
        String str = "学习";
        Rect rect = new Rect();
        this.textReviewPaint.getTextBounds(str, 0, str.length(), rect);
        float textX = (x1) - rect.centerX();// 让现实的字体处于中心位置;;
        float textY = (y1) - rect.centerY();// 让显示的字体处于中心位置;;
        canvas.drawText(str, textX, textY, textReviewPaint);

        str = "1小时";
        this.textReviewPaint.getTextBounds(str, 0, str.length(), rect);
        textX = (x2) - rect.centerX();// 让现实的字体处于中心位置;;
        textY = (y2) - rect.centerY();// 让显示的字体处于中心位置;;
        canvas.drawText(str, textX, textY, textReviewPaint);

        str = "1天";
        this.textReviewPaint.getTextBounds(str, 0, str.length(), rect);
        textX = (x3) - rect.centerX();// 让现实的字体处于中心位置;;
        textY = (y3) - rect.centerY();// 让显示的字体处于中心位置;;
        canvas.drawText(str, textX, textY, textReviewPaint);


        str = "6天";
        this.textReviewPaint.getTextBounds(str, 0, str.length(), rect);
        textX = (x4) - rect.centerX();// 让现实的字体处于中心位置;;
        textY = (y4) - rect.centerY();// 让显示的字体处于中心位置;;
        canvas.drawText(str, textX, textY, textReviewPaint);


//        if (currProgress >= 12.5) {
        if (TypeUtils.getJsonBoolean(dataValue, 1)) {
            reviewPaint.setColor(scheduleRoundColor);
        } else {
            reviewPaint.setColor(defaultRoundColor);
        }
        canvas.drawCircle(x5, y5, smallRoundWdith, reviewPaint);//20分钟


//        if (currProgress >= 37.5) {
        if (TypeUtils.getJsonBoolean(dataValue, 3)) {
            reviewPaint.setColor(scheduleRoundColor);
        } else {
            reviewPaint.setColor(defaultRoundColor);
        }
        canvas.drawCircle(x6, y6, smallRoundWdith, reviewPaint);//9小时


//        if (currProgress >= 62.5) {
        if (TypeUtils.getJsonBoolean(dataValue, 5)) {
            reviewPaint.setColor(scheduleRoundColor);
        } else {
            reviewPaint.setColor(defaultRoundColor);
        }
        canvas.drawCircle(x7, y7, smallRoundWdith, reviewPaint);//2天
//        if (currProgress >= 100) {
//            reviewPaint.setColor(scheduleRoundColor);
//        } else {
//            reviewPaint.setColor(defaultRoundColor);
//        }

//        if (currProgress >= 87.5) {
        if (TypeUtils.getJsonBoolean(dataValue, 7)) {
            reviewPaint.setColor(scheduleRoundColor);
        } else {
            reviewPaint.setColor(defaultRoundColor);
        }
        canvas.drawCircle(x8, y8, smallRoundWdith, reviewPaint);//1个月

        str = "20分钟";
        this.textReviewPaint.getTextBounds(str, 0, str.length(), rect);
        textX = (x5) - rect.centerX();// 让现实的字体处于中心位置;;
        textY = (y5) - rect.centerY();// 让显示的字体处于中心位置;;
        canvas.drawText(str, textX, textY, textReviewPaint);

        str = "9小时";
        this.textReviewPaint.getTextBounds(str, 0, str.length(), rect);
        textX = (x6) - rect.centerX();// 让现实的字体处于中心位置;;
        textY = (y6) - rect.centerY();// 让显示的字体处于中心位置;;
        canvas.drawText(str, textX, textY, textReviewPaint);


        str = "2天";
        this.textReviewPaint.getTextBounds(str, 0, str.length(), rect);
        textX = (x7) - rect.centerX();// 让现实的字体处于中心位置;;
        textY = (y7) - rect.centerY();// 让显示的字体处于中心位置;;
        canvas.drawText(str, textX, textY, textReviewPaint);

        str = "1个月";
        this.textReviewPaint.getTextBounds(str, 0, str.length(), rect);
        textX = (x8) - rect.centerX();// 让现实的字体处于中心位置;;
        textY = (y8) - rect.centerY();// 让显示的字体处于中心位置;;
        canvas.drawText(str, textX, textY, textReviewPaint);


//        canvas.drawCircle(width/2, width/2, bigRound / 2, insideProgressPaint);

        reviewPaint.setColor(bigRoundColor);
        canvas.drawCircle(width / 2, width / 2, progressRoundWdith / 2 - padding, reviewPaint);


        str = "复习";
        this.textInsidePaint.getTextBounds(str, 0, str.length(), rect);
        textX = (width / 2) - rect.centerX();// 让现实的字体处于中心位置;;
        textY = (width / 2) - rect.centerY();// 让显示的字体处于中心位置;;
        canvas.drawText(str, textX, textY, textInsidePaint);


        super.onDraw(canvas);
    }


    /**
     * 设置数据
     *
     * @param dataValue
     */
    public void setDataValue(JSONArray dataValue) {
//        currProgress = 0;
//        for (int i = 0; i < TypeUtils.getJsonArraySize(dataValue); i++) {
//            boolean isLearn = TypeUtils.getJsonBoolean(dataValue, i);
//            if (!isLearn) {
//                currProgress = ((float) i / TypeUtils.getJsonArraySize(dataValue) * 100);
//            }
//        }
        this.dataValue = dataValue;
        LogUtils.println(dataValue);
        invalidate();
    }
}
